<?php

class addorder extends AuthenticatedPage
{	  
	var $body_tpl = "page_templates/addorder.tpl";
	
  	
	function page_do()
	{
		parent::page_do();
		global $config;
		   		   
	
		if (@$_REQUEST['submit'] == "Create Order")
		{
			$custid = @$_REQUEST['custid'];
			$src = @$_REQUEST['src']; 
			$dst  = @$_REQUEST['dst'];
			$weight = @$_REQUEST['weight'];
			$year = @$_REQUEST['year'];
			$month = @$_REQUEST['month'];
			$day = @$_REQUEST['day'];
			
			if (!empty($custid) &&
				!empty($src) &&
				!empty($dst) &&
				!empty($weight) &&
				!empty($year) &&
				!empty($month) &&
				!empty($day) 
				)
			{
			
				Ice_loadProfile();
				global $ICE;

				$proxy = $ICE->stringToProxy($config['CS430 Integration']['crm orb locator']);
				$optproxy = $ICE->stringToProxy($config['CS430 Integration']['opt orb locator']);

				$crm = $proxy->ice_checkedCast("::CustomerRelationsManagement::CRM");
				$opt = $optproxy->ice_checkedCast("::Optimization::OptimizationEngine");
				
				$order = $crm->CreateOrder();

				$order->setCustomer(intval($custid));
				$order->setSrc($src);
				$order->setDst($dst);
				$order->setWeight(floatval($weight));
				
				$deadline = new CustomerRelationsManagement_datetime();
				$deadline->year = intval($year);
				$deadline->month = intval($month);
				$deadline->day = intval($day);
				
				$order->setDeadline($deadline);

				$order->save();
				@$this->body->assign_var('MSG', "<b>Order Added Succesfully</b><br>Order ID is: " . $order->getId() . "<br><br>");
				
				try {
					$opt->runTest();
				}
				catch (Exception $e)
				{
				}
			}
			else
			{
				@$this->body->assign_var('MSG', "<b>A required field was empty</b>");
				@$this->body->assign_var('custid', $custid);
				@$this->body->assign_var('src', $src);
				@$this->body->assign_var('dst', $dst);
				@$this->body->assign_var('weight', $weight);
				@$this->body->assign_var('year', $year);
				@$this->body->assign_var('month', $month);
				@$this->body->assign_var('day', $day);

			}
		}
				
	}	  
}

?>